package com.luyuan.crm.product.web;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.model.DataModel;

import org.hibernate.criterion.Order;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.product.model.Product;
import com.luyuan.crm.product.model.ProductType;
import com.luyuan.crm.product.service.ProductService;
import com.luyuan.crm.product.util.ChnToPinYinUtil;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.context.TableContext;
import com.ruijie.web.jsf.model.PageDataModel;
import com.ruijie.web.jsf.model.TableOrder;
import com.ruijie.web.jsf.model.TablePage;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;
import com.ruijie.foundation.query.criteria.RestrictionTemplate;

@Scope("view")
@Component("productAction")
public class ProductAction extends BaseAction {

    @Resource(name = "productService")
    private ProductService productService;

    private Product criteria = new Product();

    private Product product;

    private ProductType productType;

    public ProductType getProductType() {
        return productType;
    }

    public void setProductType(ProductType productType) {
        this.productType = productType;
    }

    public DataModel getTableModel() {
        TablePage limit = TableContext.getTablePage();
        List<TableOrder> tableOrders = TableContext.getTableOrders();
        List<Order> orders = new ArrayList<Order>();
        for (TableOrder tableOrder : tableOrders) {
            Order order = tableOrder.toOrder();
            if (order != null) {
                orders.add(order);
            }
        }

        CriteriaTemplate c = CriteriaTemplate.forClass(Product.class);
        RestrictionTemplate r = new RestrictionTemplate(true);
        c.add(r.like("name", criteria.getName()));
        c.add(r.like("specification", criteria.getSpecification()));
        if (criteria.getProductType() != null && criteria.getProductType().getId() != null) {
            c.add(r.eq("productType", criteria.getProductType()));
        }
        // c.add(r.like("price", criteria.getPrice()));
        Page page = this.productService.pagedQuery(c, limit.getPage(), limit.getPageSize(), orders);
        return new PageDataModel(page);
    }

    public Product getCriteria() {
        return criteria;
    }

    public Product getProduct() {
        String productId = this.getRequestParameter("productId");
        if (productId != null && !"".equals(productId)) {
            this.product = this.productService.getProductById(productId);
            this.productType = product.getProductType();
        }
        if (product == null) {
            product = new Product();
        }
        return product;
    }

    public void updateProduct() {
        this.product.setProductType(productType);
        this.productService.updateProduct(this.product);
        this.addGlobalI18nMessage("修改成功");
    }

    public void addProduct() {
        this.product.setProductType(productType);
        this.product.setAbbreviation(ChnToPinYinUtil.chinese2PinYin(this.product.getName()));
        this.productService.addProduct(product);
        this.addGlobalI18nMessage("添加成功");
    }

    public void deleteByProductId(String productId) {
        if (productId == null && "".equals(productId)) {
            this.addGlobalI18nMessage("产品ID不能为空！");
            return;
        }
        this.productService.deleteProductById(productId);
        this.addGlobalI18nMessage("删除成功");
    }

    public void batchDelete() {
        String[] selectedIds = getRequestParameterValues("id");
        if (selectedIds == null) {
            selectedIds = new String[0];
            this.addGlobalI18nMessage("请至少选择一条数据");
            return;
        }
        this.productService.batchDeleteProduct((Serializable[]) selectedIds);
        this.addGlobalI18nMessage("批量删除成功");
    }

    public void enableByProductId(String productId) {
        productService.enableByProductId(productId);
        addGlobalI18nMessage("重新销售产品成功!");
    }

    public void disableByProductId(String productId) {
        productService.disableByProductId(productId);
        addGlobalI18nMessage("停止销售产品成功!");
    }
}
